﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Power.DBUtility;//Please add references
namespace Power.DAL
{
	/// <summary>
	/// 数据访问类:Thermostat_rDAO
	/// </summary>
	public partial class Thermostat_rDAO
	{
		public Thermostat_rDAO()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string hd_id,string station_id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from thermostat_r");
			strSql.Append(" where hd_id=@hd_id and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@hd_id", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,30)			};
			parameters[0].Value = hd_id;
			parameters[1].Value = station_id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(Power.Model.Thermostat_r model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into thermostat_r(");
			strSql.Append("hd_id,station_id,evt_time,stat,t_a,t_b,t_c)");
			strSql.Append(" values (");
			strSql.Append("@hd_id,@station_id,@evt_time,@stat,@t_a,@t_b,@t_c)");
			SqlParameter[] parameters = {
					new SqlParameter("@hd_id", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,30),
					new SqlParameter("@evt_time", SqlDbType.DateTime),
					new SqlParameter("@stat", SqlDbType.VarChar,30),
					new SqlParameter("@t_a", SqlDbType.Float,8),
					new SqlParameter("@t_b", SqlDbType.Float,8),
					new SqlParameter("@t_c", SqlDbType.Float,8)};
			parameters[0].Value = model.hd_id;
			parameters[1].Value = model.station_id;
			parameters[2].Value = model.evt_time;
			parameters[3].Value = model.stat;
			parameters[4].Value = model.t_a;
			parameters[5].Value = model.t_b;
			parameters[6].Value = model.t_c;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Power.Model.Thermostat_r model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update thermostat_r set ");
			strSql.Append("evt_time=@evt_time,");
			strSql.Append("stat=@stat,");
			strSql.Append("t_a=@t_a,");
			strSql.Append("t_b=@t_b,");
			strSql.Append("t_c=@t_c");
			strSql.Append(" where hd_id=@hd_id and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@evt_time", SqlDbType.DateTime),
					new SqlParameter("@stat", SqlDbType.VarChar,30),
					new SqlParameter("@t_a", SqlDbType.Float,8),
					new SqlParameter("@t_b", SqlDbType.Float,8),
					new SqlParameter("@t_c", SqlDbType.Float,8),
					new SqlParameter("@hd_id", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,30)};
			parameters[0].Value = model.evt_time;
			parameters[1].Value = model.stat;
			parameters[2].Value = model.t_a;
			parameters[3].Value = model.t_b;
			parameters[4].Value = model.t_c;
			parameters[5].Value = model.hd_id;
			parameters[6].Value = model.station_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string hd_id,string station_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from thermostat_r ");
			strSql.Append(" where hd_id=@hd_id and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@hd_id", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,30)			};
			parameters[0].Value = hd_id;
			parameters[1].Value = station_id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Power.Model.Thermostat_r GetModel(string hd_id,string station_id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 hd_id,station_id,evt_time,stat,t_a,t_b,t_c from thermostat_r ");
			strSql.Append(" where hd_id=@hd_id and station_id=@station_id ");
			SqlParameter[] parameters = {
					new SqlParameter("@hd_id", SqlDbType.VarChar,50),
					new SqlParameter("@station_id", SqlDbType.VarChar,30)			};
			parameters[0].Value = hd_id;
			parameters[1].Value = station_id;

			Power.Model.Thermostat_r model=new Power.Model.Thermostat_r();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Power.Model.Thermostat_r DataRowToModel(DataRow row)
		{
			Power.Model.Thermostat_r model=new Power.Model.Thermostat_r();
			if (row != null)
			{
				if(row["hd_id"]!=null)
				{
					model.hd_id=row["hd_id"].ToString();
				}
				if(row["station_id"]!=null)
				{
					model.station_id=row["station_id"].ToString();
				}
				if(row["evt_time"]!=null && row["evt_time"].ToString()!="")
				{
					model.evt_time=DateTime.Parse(row["evt_time"].ToString());
				}
				if(row["stat"]!=null)
				{
					model.stat=row["stat"].ToString();
				}
				if(row["t_a"]!=null && row["t_a"].ToString()!="")
				{
					model.t_a=decimal.Parse(row["t_a"].ToString());
				}
				if(row["t_b"]!=null && row["t_b"].ToString()!="")
				{
					model.t_b=decimal.Parse(row["t_b"].ToString());
				}
				if(row["t_c"]!=null && row["t_c"].ToString()!="")
				{
					model.t_c=decimal.Parse(row["t_c"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select hd_id,station_id,evt_time,stat,t_a,t_b,t_c ");
			strSql.Append(" FROM thermostat_r ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" hd_id,station_id,evt_time,stat,t_a,t_b,t_c ");
			strSql.Append(" FROM thermostat_r ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM thermostat_r ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.station_id desc");
			}
			strSql.Append(")AS Row, T.*  from thermostat_r T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "thermostat_r";
			parameters[1].Value = "station_id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

